package com.servlet;
import com.model.Flight;
import com.service.FlightService;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/FlightServlet")
public class FlightServlet extends HttpServlet {
    FlightService cs = new FlightService();
    HttpServletRequest request;
    HttpServletResponse response;

    @SneakyThrows
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.request = request;
        this.response = response;
        String me = request.getParameter("method");
        if (me.equals("show")) {
            this.show();
        }
        if (me.equals("add")) {
            this.add();
        }
        if (me.equals("delete")){
            this.delete();
        }
        if (me.equals("toupdate")){
            this.toupdate();
        }
        if (me.equals("update")){
            this.update();
        }
    }
    private void update() throws IOException {
        String id = request.getParameter("id");
        String num = request.getParameter("num");
        String splace = request.getParameter("splace");
        String splaces = request.getParameter("splaces");
        String stime = request.getParameter("stime");
        String etime = request.getParameter("etime");
        String mprice = request.getParameter("mprice");
        String seat = request.getParameter("seat");
        String flightnum = request.getParameter("flightnum");
        cs.update(id,num,splace,splaces,stime,etime,mprice,seat,flightnum);
//        response.setContentType("text/html; charset=UTF-8");
//        response.getWriter().print("<script>parent.showPersonal()</script>");
    }
    private void toupdate() throws ServletException, IOException, SQLException {
        int id = Integer.parseInt(request.getParameter("id"));
        Flight flight = cs.selectById(id);
        request.setAttribute("flights",flight);
        request.getRequestDispatcher("flight/update.jsp").forward(request, response);
    }
    private void show() throws ServletException, IOException {
        List<Flight> flights = cs.show();
        System.out.println(flights.toString());
        request.setAttribute("flights", flights);
        request.getRequestDispatcher("flight/show.jsp").forward(request, response);
    }
    private void add() throws IOException {
        String num = request.getParameter("num");
        String splace = request.getParameter("splace");
        String splaces = request.getParameter("splaces");
        String stime = request.getParameter("stime");
        String etime = request.getParameter("etime");
        String mprice = request.getParameter("mprice");
        String seat = request.getParameter("seat");
        String flightnum = request.getParameter("flightnum");
        cs.add(num,splace,splaces,stime,etime,mprice,seat,flightnum);
    }
    private void delete() throws ServletException, IOException {
        String id = request.getParameter("id");
        cs.delete(id);
        this.show();
    }
}